package com.feng.study.util;

import com.github.stuxuhai.jpinyin.PinyinException;
import com.github.stuxuhai.jpinyin.PinyinFormat;
import com.github.stuxuhai.jpinyin.PinyinHelper;
import com.google.common.collect.Lists;
import com.vip.vjtools.vjkit.number.RandomUtil;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang.StringUtils;

import java.util.List;
import java.util.Random;
import java.util.regex.Pattern;

@Slf4j
public class ChineseToPinyinUtil {
    private static final Integer max = 2;

    /**
     * 全拼 && 首字母拼音 若中文串中多音字少于等于5个，组合成所有全拼音所有首字母拼音 若中文串中多音字多余5个，则不去处理多音字组合 注意：拼音不做声调的区分
     */
    public static List<String> converterToAllSpell(String chines) {
        if (StringUtils.isBlank(chines) || "".equals(chines.trim())) {
            return Lists.newArrayList();
        }
        String lowerCase = chines.trim().toLowerCase();
        /** ^[\u2E80-\u9FFF]+$ 匹配所有东亚区的语言 ^[\u4E00-\u9FFF]+$ 匹配简体和繁体 ^[\u4E00-\u9FA5]+$ 匹配简体 */
        String regExp = "^[\u4E00-\u9FA5]+$";
        Pattern pattern = Pattern.compile(regExp);
        // 全拼
        List<List<String>> spell = Lists.newArrayList();
        // 首字母拼音
        List<List<String>> firstSpell = Lists.newArrayList();
        // 记录多音字的个数(声调不同的不算)
        int count = 0;
        // 记录中文字数
        int chineseCount = 0;

        for (int i = 0; i < lowerCase.length(); i++) {
            char unit = lowerCase.charAt(i);
            List<String> temp = Lists.newArrayList();
            List<String> firstTemp = Lists.newArrayList();
            // 是否为汉字
            if (pattern.matcher(String.valueOf(unit)).find() && count <= max) {
                chineseCount++;
                String[] pinyins = PinyinHelper.convertToPinyinArray(unit, PinyinFormat.WITHOUT_TONE);
                if (pinyins != null) {
                    for (String pinyin : pinyins) {
                        if (!temp.contains(pinyin)) {
                            temp.add(pinyin);
                            firstTemp.add(String.valueOf(pinyin.charAt(0)));
                        }
                    }
                    count = temp.size() > 1 ? count + 1 : count;
                }
            } else {
                temp.add(String.valueOf(unit));
                firstTemp.add(String.valueOf(unit));
            }
            spell.add(temp);
            firstSpell.add(firstTemp);
        }
        List<String> result = Lists.newArrayList();
        // 没有中文字符，不用处理字符串
        if (chineseCount == 0) {
            return Lists.newArrayList(lowerCase);
        }
        // 多音字多于五个，不去处理多音字组合
        if (count > max) {
            return multiPinyin(lowerCase);
        }
        result.addAll(parseTheChineseByObject(spell));
        result.addAll(parseTheChineseByObject(firstSpell));
        return result;
    }

    /**
     * 多音字多于五个的直接取一个全拼 && 一个首字母拼音
     */
    private static List<String> multiPinyin(String lowerCase) {
        String str = lowerCase.replaceAll(",", "");
        System.out.println(str);
        List<String> result = Lists.newArrayList();
        String toPinyinString = "";
        try {
            toPinyinString = PinyinHelper.convertToPinyinString(str, ",", PinyinFormat.WITHOUT_TONE);
            System.out.println(toPinyinString);
        } catch (PinyinException e) {
            log.error("汉字转拼音失败,chines={}", lowerCase, e);
        }
        if (StringUtils.isNotEmpty(toPinyinString)) {
            String[] split = toPinyinString.split(",");
            StringBuilder firstPinyin = new StringBuilder();
            StringBuilder pinyin = new StringBuilder();
            for (String s : split) {
                firstPinyin.append(s.charAt(0));
                pinyin.append(s);
            }
            result.add(firstPinyin.toString());
            result.add(pinyin.toString());
        }
        return result;
    }

    /**
     * 组装拼音字符串
     */
    private static List<String> parseTheChineseByObject(List<List<String>> target) {
        List<String> result = Lists.newArrayList();
        for (List<String> list : target) {
            List<String> temp = Lists.newArrayList();
            if (CollectionUtils.isNotEmpty(result)) {
                for (String s : list) {
                    for (String s1 : result) {
                        temp.add(s1 + s);
                    }
                }
            } else {
                for (String s : list) {
                    temp.add(s);
                }
            }
            if (temp != null && temp.size() > 0) {
                result = temp;
            }
        }
        return result;
    }

    public static void main(String[] args) {
        String str = "小糸";
        long start = System.currentTimeMillis();
        List<String> list = converterToAllSpell(str);
        long time = System.currentTimeMillis() - start;
        System.out.println("耗时：" + time);
        System.out.println(list);
        System.out.println(list.size());

        int i = RandomUtil.nextInt(100);
    }
}
